home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Bezier -- Support for Bezier curves
- **
- ** Herein reside support routines for drawing Bezier curves.
- **
- ** Copyright (C) 1987, 1988 David W. Smith
- ** Submitted to MacTutor for their source-disk.
- */
-
- #include <MacTypes.h>
-
-
- /*
- * The greater the number of curve segments, the smoother the curve,
- * and the longer it takes to generate and draw. The number below was
- * pulled out of a hat, and seems to work o.k.
- */
- #define SEGMENTS 16
-
- static Fixed weight1[SEGMENTS + 1];
- static Fixed weight2[SEGMENTS + 1];
-
- #define w1(s) weight1[s]
- #define w2(s) weight2[s]
- #define w3(s) weight2[SEGMENTS - s]
- #define w4(s) weight1[SEGMENTS - s]
-
-
-
- /*
- * SetupBezier -- one-time setup code.
- *
- * Compute the weights for the Bezier function.
- *
- * For the those concerned with space, the tables can be precomputed.
- * The setup is done here for purposes of illustration.
- */
- void
- SetupBezier()
- {
- Fixed t, zero, one;
- int s;
-
- zero = FixRatio(0, 1);
- one = FixRatio(1, 1);
-
- weight1[0] = one;
- weight2[0] = zero;
-
- for ( s = 1 ; s < SEGMENTS ; ++s )
- {
- t = FixRatio(s, SEGMENTS);
-
- weight1[s] = FixMul(one - t, FixMul(one - t, one - t));
- weight2[s] = 3 * FixMul(t, FixMul(t - one, t - one));
- }
-
- weight1[SEGMENTS] = zero;
- weight2[SEGMENTS] = zero;
- }
-
-
- /*
- * computeSegments -- compute segments for the Bezier curve
- *
- * Compute the segments along the curve.
- *
- * The curve touches the endpoints, so don't bother to compute them.
- */
- static void
- computeSegments(p1, p2, p3, p4, segment)
- Point p1, p2, p3, p4;
- Point segment[];
- {
- int s;
-
- segment[0] = p1;
-
- for ( s = 1 ; s < SEGMENTS ; ++s )
- {
- segment[s].v = FixRound(w1(s) * p1.v + w2(s) * p2.v +
- w3(s) * p3.v + w4(s) * p4.v);
-
- segment[s].h = FixRound(w1(s) * p1.h + w2(s) * p2.h +
- w3(s) * p3.h + w4(s) * p4.h);
- }
-
- segment[SEGMENTS] = p4;
- }
-
-
- /*
- * BezierCurve -- Draw a Bezier Curve
- *
- * Draw a curve with the given endpoints (p1, p4), and the given
- * control points (p2, p3).
- *
- * Note that we make no assumptions about the pen or pen mode.
- */
- void
- BezierCurve(p1, p2, p3, p4)
- Point p1, p2, p3, p4;
- {
- int s;
- Point segment[SEGMENTS + 1];
-
- computeSegments(p1, p2, p3, p4, segment);
-
- MoveTo(segment[0].h, segment[0].v);
-
- for ( s = 1 ; s <= SEGMENTS ; ++s )
- {
- if ( segment[s].h != segment[s - 1].h ||
- segment[s].v != segment[s - 1].v )
- {
- LineTo(segment[s].h, segment[s].v);
- }
- }
- }
-
-